package com.yjc.hire.work.controller;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.yjc.bo.EditWorkExpBO;
import com.yjc.bo.ModifyCompanyInfoBO;
import com.yjc.hire.base.BaseInfoProperties;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.ResponseStatusEnum;
import com.yjc.hire.utils.LocalDateUtils;
import com.yjc.hire.work.bo.EditResumeBO;
import com.yjc.hire.work.service.ResumeSearchService;
import com.yjc.hire.work.service.ResumeService;
import com.yjc.hire.work.vo.ResumeVO;
import com.yjc.pojo.ResumeProjectExp;
import com.yjc.pojo.ResumeWorkExp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.stringtemplate.v4.ST;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/7/19
 * </p>
 */

@RequestMapping("resume")
@RestController
@Slf4j
public class ResumeController extends BaseInfoProperties {
    @Resource
    private ResumeService resumeService;

    @Autowired
    private ResumeSearchService resumeSearchService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("init")
    public GraceJSONResult initResume( @RequestParam("userId") String userId){
        resumeService.initResume(userId);
        return GraceJSONResult.ok();
    }

    @PostMapping("modify")
    public GraceJSONResult mdiModify(@RequestBody EditResumeBO editResumeBO){
        resumeService.modifyResume(editResumeBO);
        return GraceJSONResult.ok();
    }

    @PostMapping("queryMyResume")
    public GraceJSONResult queryMyResume(@RequestParam("userId") String userId){
        if (StringUtils.isBlank(userId)) return GraceJSONResult.error();
        ResumeVO resumeInfo = new ResumeVO();
        String resumeJson = stringRedisTemplate.opsForValue().get(REDIS_RESUME_INFO + ":" + userId);
        if (StringUtils.isBlank(resumeJson)){
            resumeInfo = new Gson().fromJson(resumeJson, ResumeVO.class);

            return GraceJSONResult.ok(resumeInfo);
        }
        resumeInfo = resumeService.getResumeInfo(userId);
        stringRedisTemplate.opsForValue().set(REDIS_RESUME_INFO + ":" + userId, new Gson().toJson(resumeInfo));
        return GraceJSONResult.ok(resumeInfo);
    }
    @PostMapping("editWorkExp")
    public GraceJSONResult editWorkExp(@RequestBody EditWorkExpBO editWorkExpBO){
        resumeService.editWorkExp(editWorkExpBO);
        return GraceJSONResult.ok();
    }

    @PostMapping("getWorkExp")
    public GraceJSONResult getWorkExp(String workWxpId,String userId){
        if (StringUtils.isBlank(workWxpId) || StringUtils.isBlank(userId)) return GraceJSONResult.error();
        ResumeWorkExp workExp = resumeService.getWorkExp(workWxpId, userId);
        return GraceJSONResult.ok(workExp);
    }

    @PostMapping("deleteWorkExp")
    public GraceJSONResult deleteWorkExp(String workWxpId,String userId){
        if (StringUtils.isBlank(workWxpId) || StringUtils.isBlank(userId)) return GraceJSONResult.error();
        resumeService.deleteWorkExp(workWxpId,userId);
        return GraceJSONResult.ok();
    }

    @PostMapping("getProjectExp")
    public GraceJSONResult getProjectExp(String projectExpId,String userId){
        if (StringUtils.isBlank(projectExpId) || StringUtils.isBlank(userId)) return GraceJSONResult.error();
        ResumeProjectExp exp = resumeService.getProjectExp(projectExpId,userId);
        return GraceJSONResult.ok(exp);
    }

    @PostMapping("refresh")
    public GraceJSONResult refresh(String userId,String resumeId){
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(resumeId)) return GraceJSONResult.error();
        int maxRefreshCount = 3;
        String today = LocalDateUtils.getLocalDateStr();
        int userAlready = 1;
        String useAlreadyRefreshCount = stringRedisTemplate.opsForValue().get(USER_ALREADY_REFRESHED_COUNTS + ":" + today + ":" + userId);
        if (StringUtils.isNotBlank(useAlreadyRefreshCount)){
            stringRedisTemplate.opsForValue().set(USER_ALREADY_REFRESHED_COUNTS + ":" + today + ":" + userId, useAlreadyRefreshCount,1, TimeUnit.DAYS);
        }else {
            assert useAlreadyRefreshCount != null;
            userAlready = Integer.parseInt(useAlreadyRefreshCount);
        }
        if (userAlready <= maxRefreshCount){
            resumeService.resumeRefresh(userId, resumeId);
            stringRedisTemplate.opsForValue().set(USER_ALREADY_REFRESHED_COUNTS + ":" + today + ":" + userId,
                    userAlready + 1 + "",1,TimeUnit.DAYS);
        }else {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.RESUME_MAX_LIMIT_ERROR);
        }
        return GraceJSONResult.ok();
    }
}
